package kaang.dev.warpimage;

import android.graphics.Bitmap;

public class ImageProcesses {
	public static Bitmap Swirl(Bitmap bmp, int pointX, int pointY, WarpingMatrix transform, int areaID, int intensityID)
	{
		Bitmap edited = bmp.copy(Bitmap.Config.ARGB_8888, true);
		int w = bmp.getWidth();
		int h = bmp.getHeight();
		int[] srcPixels = new int[w*h];
		int[] destPixels = new int[w*h];
		bmp.getPixels(srcPixels, 0, w, 0, 0, w, h);
		edited.getPixels(destPixels, 0, w, 0, 0, w, h);
		
		float[] matrixX = transform.TransformMatrixX();
		float[] matrixY = transform.TransformMatrixY();
		
		int x, y, id, srcID, desID, newX, newY;
		int area = transform.GetArea() * w / 100;
		int radius = area / 2;
		for(int i = -radius; i<radius ; i++)
		{
			for(int j= -radius; j<radius ; j++)
			{
				x = pointX + i;
				y = pointY + j;
				
				if(x < 0 || x >= w || y<0 || y>=h)
					continue;
				
				id = (j + radius) * area + (i + radius);
				newX = x + Math.round(matrixX[id]);
				newY = y + Math.round(matrixY[id]);
				desID = y * w + x;
				srcID = newY * w + newX;
				destPixels[desID] = srcPixels[srcID];
			}
		}
		
		edited.setPixels(destPixels, 0, w, 0, 0, w, h);
		return edited;
	}
}
